package net.lcheng.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.lcheng.commons.utils.PageList;
import net.lcheng.dao.WxShopCategoryMapper;
import net.lcheng.dao.WxShopMapper;
import net.lcheng.model.WxShop;
import net.lcheng.model.WxShopCategory;
import net.lcheng.model.WxShopCategoryExample;
import net.lcheng.model.WxShopExample;
import net.lcheng.service.WxShopService;
import net.lcheng.vo.WxShopModel;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
 * Created by LV on 2016/6/30.
 */
@Service("wxShopService")
public class WxShopServiceImpl implements WxShopService {
    @Resource
    private WxShopMapper shopMapper;
    @Resource
    private WxShopCategoryMapper shopCategoryMapper;
    /**
     * 由shopId查询微信商店的分类列表
     * @param shopId
     * @return
     **/
    @Override
    public List<WxShopCategory> getShopCategoryList(int shopId) {
        WxShopCategoryExample example=new WxShopCategoryExample();
        example.createCriteria().andShopIdEqualTo(shopId);
        example.setOrderByClause("order_num DESC");
        List<WxShopCategory> list=shopCategoryMapper.selectByExample(example);
        if(list!=null&&list.size()>0)
            return list;
           return null;
    }
    /**
     * 由用户id查找商店信息
     * @param userId
     * @return
     */
    @Override
    public WxShop getShopInforByUserId(String userId) {
        WxShopExample example=new WxShopExample();
        example.createCriteria().andUserUserIdEqualTo(userId);
        List<WxShop> list=shopMapper.selectByExample(example);
        if (list!=null&&list.size()>0){
            return list.get(0);
        }else
        return null;
    }

    /**
     * 由用户id和店铺状态查找商店信息
     * @param userId 用户ID
     * @param shopStatus 店铺状态
     * @return
     */
    @Override
    public WxShop getShopInforByUserIdAndShopStatus(String userId, int shopStatus) {
        WxShopExample example=new WxShopExample();
        example.createCriteria().andUserUserIdEqualTo(userId)
                .andShopStatusEqualTo(shopStatus);
        List<WxShop> list=shopMapper.selectByExample(example);
        if (list!=null&&list.size()>0){
            return list.get(0);
        }else
            return null;
    }

    @Override
    public int updateName(String shopName,int shopId) {
        WxShop shop =  new WxShop();
        shop.setShopName(shopName);
        shop.setShopId(shopId);
        return shopMapper.updateByPrimaryKeySelective(shop);
    }
    @Override
    public int update(WxShop wxShop){
       return shopMapper.updateByPrimaryKeySelective(wxShop);
    }
    @Override
    public WxShop getWxShopByShopId(Integer wxShopShopId) {
        return shopMapper.selectByPrimaryKey(wxShopShopId);
    }
    @Override
    public PageList<WxShopModel> getWxShopList(Integer pageIndex, Integer pageSize, String shopName, String userName, Date sdate, Date edate) {
        PageHelper.startPage(pageIndex,pageSize);
        WxShopExample example=new WxShopExample();
        WxShopExample.Criteria criteria = example.createCriteria();
//        if(StringUtils.isEmpty(shopName) == false) {
//            criteria.andShopNameLike(shopName);
//        }
//        if (sdate!=null && edate!=null){
//            criteria.andShopStartTimeBetween(sdate,edate);
//        }
//        if(sdate != null&& edate==null ) {
//            criteria.andShopStartTimeGreaterThanOrEqualTo(sdate);
//        }
//        if (edate!=null && sdate==null){
//            criteria.andShopStartTimeLessThanOrEqualTo(edate);
//        }
//        example.or(criteria);
//        example.setOrderByClause("user_id desc");
//        List<WxShop>list=shopMapper.selectByExample(example);
        List<WxShopModel> list=shopMapper.getWxShopList(shopName,userName,sdate,edate);
        PageInfo pageInfo = new PageInfo(list);
        PageList pageList = new PageList<WxShopModel>();
        pageList.list = pageInfo.getList();
        pageList.totalCount = pageInfo.getTotal();
        pageList.totalPage=pageInfo.getPages();
        pageList.pageIndex=pageInfo.getPageNum();
        pageList.pageSize=pageInfo.getPageSize();
        return pageList;
    }
    /***
     * 申请开微店
     * @return
     */
    @Override
    public int insert(String shopName, String shopUserUserId, String name, String phone, String addrProvinceCode, String addrProvinceName, String addrCityCode, String addrCityName, String addrDistrictCode, String addrDistrictName,String shopSummary,String shopMainProduct) {
        WxShop wxShop=new WxShop();
        wxShop.setShopUid(UUID.randomUUID().toString());
        wxShop.setCreateOn(new Date());
        wxShop.setPayStatus(0);
        wxShop.setShopStatus(1);
        wxShop.setApplyStatus(1);
        wxShop.setShopName(shopName);
        wxShop.setUserUserId(shopUserUserId);
        wxShop.setName(name);
        wxShop.setPhone(phone);
        wxShop.setAddrProvinceCode(addrProvinceCode);
        wxShop.setAddrProvinceName(addrProvinceName);
        wxShop.setAddrCityCode(addrCityCode);
        wxShop.setAddrCityName(addrCityName);
        wxShop.setAddrDistrictName(addrDistrictName);
        wxShop.setAddrDistrictCode(addrDistrictCode);
        wxShop.setShopSummary(shopSummary);
        wxShop.setShopMainProduct(shopMainProduct);
        wxShop.setApplyTime(new Date());
        return shopMapper.insertSelective(wxShop);
    }
}